#!/bin/bash
# harbor 私有库地址和项目
HARBOR=harbor.ciutopia.com/xbedu-release
NEW_TAG=1.2.5
echo ": 进入docker目录，删除 历史文件 "

# 清空docker目录下可能的历史残留文件
if [  -d  "dist" ];  then
   rm -rf dist
   echo ": 删除 历史文件dist"
fi

echo "： 返回到上层根目录"
cd ..
pwd

echo "： move dist目录到docker目录下"
sudo mv dist docker

echo "： dist成功转移到docker目录下"


sudo docker -v

echo "： 停止历史容器，删除历史容器和镜像"
CID=$(docker ps -a | grep "ixiaoyuan-yuannei" | awk '{print $1}')
if [ -n "$CID" ]; then
  echo "存在 ixiaoyuan-yuannei 容器，CID=$CID,删除docker容器 ..."
  docker stop $CID
  sleep 1
  docker rm -f  $CID
  echo "成功删除 ixiaoyuan-yuannei 容器  $CID"
else
  echo "不存在 ixiaoyuan-yuannei 容器"
fi
sleep 1
#镜像id
IID=$(docker images | grep "ixiaoyuan-yuannei" | awk '{print $3}')
if [ -n "$IID" ]; then
  echo "存在 ixiaoyuan-yuannei 镜像，IID=$IID ，准备强制删除 "
  docker rmi -f $IID
  echo "成功删除 ixiaoyuan-yuannei $IID 镜像"
fi

sleep 1

echo "： 进入到docker目标目录 "
cd docker
pwd

#echo "：  创建测试镜像 "
#docker build -t  ${HARBOR}/xbedu-ixiaoyuan-yuannei:${NEW_TAG}.test --build-arg envType=test .
#sleep 3
#echo "：  推送镜像到私有库  ${HARBOR}"
##docker tag ixiaoyuan-yuannei:${NEW_TAG} ${HARBOR}/ixiaoyuan-yuannei:${NEW_TAG}
#docker push ${HARBOR}/xbedu-ixiaoyuan-yuannei:${NEW_TAG}.test
#echo "推送到harbor后删除掉新构建的镜像"
#docker rmi  ${HARBOR}/xbedu-ixiaoyuan-yuannei:${NEW_TAG}.test

echo "：  创建Release镜像 "
docker build -t  ${HARBOR}/xbedu-ixiaoyuan-yuannei:${NEW_TAG} --build-arg envType=prod .
sleep 3
echo "：  推送镜像到私有库  ${HARBOR}"
docker push ${HARBOR}/xbedu-ixiaoyuan-yuannei:${NEW_TAG}
echo "推送到harbor后删除掉新构建的镜像"
docker rmi  ${HARBOR}/xbedu-ixiaoyuan-yuannei:${NEW_TAG}



DATE=`date +%Y%m%d%H%M`
# 备份历史版本
#mkdir -p /data/xbedu-targets-backup/$DATE-xbedu-admin
#
#mv dist /data/xbedu-targets-backup/$DATE-xbedu-admin
